#include "route_train_dialog.h"
#include "ui_route_train_dialog.h"
#include "static_functions.h"
#include "positionmanager.h"
#include "route_trainer_manager.h"
#include <iostream>
#include <QInputDialog>
#include <QSettings>
#include <QDebug>
#include <QDateTime>

RouteTrainDialog* RouteTrainDialog::instance = nullptr;


RouteTrainDialog::RouteTrainDialog(PositionManager* positionManager, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RouteTrainDialog),
    positionManager(positionManager)
{
    ui->setupUi(this);


    //设置列数
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setColumnWidth(0, 200);
    ui->tableWidget->setColumnWidth(1, 100);
    ui->tableWidget->setColumnWidth(2, 100);
    ui->tableWidget->setColumnWidth(3, 120);

    QTableWidgetItem *item0 = new QTableWidgetItem;
    QTableWidgetItem *item1 = new QTableWidgetItem;
    QTableWidgetItem *item2 = new QTableWidgetItem;
    QTableWidgetItem *item3 = new QTableWidgetItem;

    //设置显示内容
    item0->setText("分段终点名称");
    item1->setText("到达时间");
    item2->setText("分段花费时间");
    item3->setText("停顿时间(攀爬也算)");

    //设置水平表头
    ui->tableWidget->setHorizontalHeaderItem(0, item0);
    ui->tableWidget->setHorizontalHeaderItem(1, item1);
    ui->tableWidget->setHorizontalHeaderItem(2, item2);
    ui->tableWidget->setHorizontalHeaderItem(3, item3);

    instance = this;

    on_btn_start_clicked();
}

RouteTrainDialog::~RouteTrainDialog()
{
    routeTrainerManager.endTrainByOtherThread(false);
    if (ui != NULL) {
        delete ui;
        ui = NULL;
    }

}

void RouteTrainDialog::closeEvent(QCloseEvent *event)
{
    StaticFunctions::noticeRouteTrainDialogClosed();
    QDialog::closeEvent(event);
}




void RouteTrainDialog::on_btn_start_clicked()
{
    ui->btn_start->setDisabled(true);
    ui->btn_start->setText("已开始");
    routeTrainerManager.startTrain(positionManager);
}
void RouteTrainDialog::on_btn_end_clicked()
{
    QList<RouteTrainDisplayInfo*> * list = routeTrainerManager.endTrainByOtherThread(true);
    displayRouteTrainResult(list, true);
}

void RouteTrainDialog::displayRouteTrainResult(QList<RouteTrainDisplayInfo*> * list, bool isEnd) {
    // 删除之前的
    for (int rowIdx=ui->tableWidget->rowCount()-1; rowIdx>=0; --rowIdx) {
        ui->tableWidget->removeRow(rowIdx);
    }


    //初始化数据
    //添加数据时，一定要设置行数，否则数据不会展示出来
    ui->tableWidget->setRowCount(list->length());

    // 循环遍历
    for (int rowIdx=0; rowIdx<list->length(); ++rowIdx) {
        const RouteTrainDisplayInfo* info = list->at(rowIdx);

        QTableWidgetItem* item0 = new QTableWidgetItem;
        item0->setText(info->endPosName);
        ui->tableWidget->setItem(rowIdx, 0, item0);

        QTableWidgetItem* item1 = new QTableWidgetItem;
        item1->setText(info->reachEndTimeStr);
        ui->tableWidget->setItem(rowIdx, 1, item1);

        QTableWidgetItem* item2 = new QTableWidgetItem;
        item2->setText(info->spendTimeStr);
        ui->tableWidget->setItem(rowIdx, 2, item2);

        QTableWidgetItem* item3 = new QTableWidgetItem;
        item3->setText(StaticFunctions::spendMSToString(info->climbOrStopTimeMS));
        ui->tableWidget->setItem(rowIdx, 3, item3);
    }


    if (isEnd) {
        ui->btn_start->setDisabled(false);
        ui->btn_start->setText("开始");
        ui->btn_end->setDisabled(true);
        ui->btn_end->setText("已完成");
    } else {
        ui->btn_start->setDisabled(true);
        ui->btn_start->setText("训练中");
        ui->btn_end->setDisabled(false);
        ui->btn_end->setText("结束");
    }
}
